package com.example.springbootdemo.mall.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootdemo.controller.BaseController;
import com.example.springbootdemo.mall.entity.ForTest;
import com.example.springbootdemo.mall.entity.OrderProduct;
import com.example.springbootdemo.mall.entity.OrderProductItem;
import com.example.springbootdemo.mall.entity.ext.OrderProductItemExcle;
import com.example.springbootdemo.mall.service.OrderProductItemService;
import com.example.springbootdemo.mall.service.OrderProductService;
import com.example.springbootdemo.utils.*;
import com.example.springbootdemo.utils.excle.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags = "前台端 商城订单相关接口")
@Slf4j
@Validated
@RestController
@RequestMapping("/app/mall/order")
public class OrderProductController extends BaseController {
    @Autowired
    OrderProductService orderProductService;
    @Autowired
    OrderProductItemService orderProductItemService;

    
    @PostMapping("/test")
    public void aaa(@RequestBody ForTest forTest){
        System.out.println(forTest);
    }
    
    @ApiOperation("获取对应用户自己的订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", required = true, value = "页数", paramType = "query"),
            @ApiImplicitParam(name = "limit", required = true, value = "每页条数", paramType = "query"),
            @ApiImplicitParam(name = "status", required = true, value = " created 订单创建  paid 已支付  confirmed 第三方确认有货 delivered 已发货 signed 确认收货 success 订单完成 cancel 取消订单 closed 订单关闭", paramType = "query"),
    })
    @GetMapping("/list")
    public WebResult getlist(@RequestParam Integer page, @RequestParam Integer limit,HttpServletRequest request) {
        QueryWrapper<OrderProduct> queryWrapper= new QueryWrapper<OrderProduct>()
                .eq("user_id", WebUtil.getUid(request));
        PageUtils<OrderProduct> list = orderProductService.queryForPage(page, limit);
        return data(list);
    }

    @ApiOperation("下载子订单excle")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payStartTime", required = false, value = "支付时间起始时间", paramType = "query"),
            @ApiImplicitParam(name = "payEndTime", required = false, value = "支付时间结束时间", paramType = "query"),
            @ApiImplicitParam(name = "page", required = true, value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "limit", required = true, value = "一页显示的条数", paramType = "query")
    })
    @GetMapping("/downloadexcle")
    public void downloadFailedUsingJson(@RequestParam Integer page, @RequestParam Integer limit, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime payStartTime,
                                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime payEndTime,
                                        HttpServletResponse response) throws IOException {

        //根据条件查询
        QueryWrapper<OrderProductItem> queryWrapper = new QueryWrapper<OrderProductItem>()
                .ge(payStartTime!=null,"gmt_pay",payStartTime)
                .le(payEndTime!=null,"gmt_pay",payEndTime);

        PageUtils<OrderProductItem> orderProductItemPageUtils = orderProductItemService.queryForPage(page, limit,queryWrapper);
        List<OrderProductItem> orderProductItems = orderProductItemPageUtils.getList();
        //将userid和settlementSharePrice 变为string
        List<OrderProductItemExcle> orderProductItemExcles = BeanConvertUtil.copyList(orderProductItems, OrderProductItemExcle.class);
        //excel文件名
        String fileName = "订单详情" + System.currentTimeMillis();
        try {
            EasyExcelUtil.writeExcel(response, fileName, orderProductItemExcles, OrderProductItemExcle.class, true, "订单详情");
        } catch (Exception e) {
            log.info("导出数据到execl出错");
            log.error("{}", e.getMessage(), e);
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("status", "failure");
//            map.put("message", "下载文件失败" + e.getMessage());
//            response.getWriter().println(JSON.toJSONString(map));

        }

    }
}
